//
//  NetworkTool.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/6.
//  Copyright © 2017年 tml. All rights reserved.
//

import Foundation
import SwiftyJSON
import Alamofire
import NotificationBannerSwift
enum MethodType {
    case get
    case post
}

protocol NetworkToolProtocol {
    //获取首页推荐数据
    static func loadHomeRecommendData(sexType: Int, completionHandle: @escaping (RecommentModel) -> ())
    
    //获取首页排行数据
    static func loadHomeRankingData(completionHandle: @escaping (RankinglistModel) -> ())
    
    //获取首页订阅数据
    static func loadHomeSubscribeData(completionHandle: @escaping (SubscribeListModel) -> ())
    
    //获取首页VIP数据
    static func loadHomeVIPData(completionHandle: @escaping (VIPListModel) -> ())
    
    //分类数据
    static func loadCateData(completionHandle: @escaping (CateModel) -> ())
    
    //漫画列表
    static func loadComicList(argCon: Int, argName: String, argValue: Int,  page: Int,completionHandle: @escaping (ComiscTypeListModel) -> () )
    
    //漫画详情 ===
    //猜你喜欢
    static func loadLoveData(comicid: Int, completionHandle: @escaping (LoveModel) -> ())
    //详情（基本）
    static func loadDetailStaticData(comicid: Int, completionHandle: @escaping (DetailStaticModel) -> ())
    
    //详情（实时）
    static func loadDetailRealtimeData(comicid: Int, completionHandle: @escaping (DetailRealtimeModel) -> ())
    
    //评论
    static func loadCommentData(object_id: Int, thread_id: Int, page: Int, completionHandle: @escaping (CommentListModel) -> ())
    
    //章节内容
    static func loadChapterData(chapter_id: Int, completionHandle: @escaping (ChapterModel) -> ())
    
    static func warnBar(with string: String)
}

class NetworkTool: NetworkToolProtocol {
    
    //获取首页推荐数据
    static func loadHomeRecommendData(sexType: Int, completionHandle: @escaping (RecommentModel) -> ()) {
        let url = BASE_URL + "/comic/boutiqueListNew"
        let requestParmeters:[String: Any] = ["time": Int32(Date().timeIntervalSince1970),
        "device_id": UIDevice.current.identifierForVendor!.uuidString,
        "key":requestKey,
        "model":UIDevice.current.modelName,
        "target":"U17_3.0",
        "version":versionCode,
        "sexType":sexType,
        "v":3320101]
        Alamofire.request(url, method:.get, parameters: requestParmeters).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let recommendModel = RecommentModel(jsonData: json["data"]["returnData"])
                completionHandle(recommendModel)
            }
        }
    }

    //获取首页排行数据
    static func loadHomeRankingData(completionHandle: @escaping (RankinglistModel) -> ()) {
        let url = BASE_URL + "/rank/list"
        Alamofire.request(url, method:.get).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let rankingModel = RankinglistModel(jsonData: json["data"]["returnData"])
                completionHandle(rankingModel)
            }
        }
    }
    
    //获取首页订阅数据
    static func loadHomeSubscribeData(completionHandle: @escaping (SubscribeListModel) -> ()) {
        let url = BASE_URL + "/list/newSubscribeList"
        Alamofire.request(url, method:.get).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = SubscribeListModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //获取首页VIP数据
    static func loadHomeVIPData(completionHandle: @escaping (VIPListModel) -> ()) {
        let url = BASE_URL + "/list/vipList"
        Alamofire.request(url, method:.get).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = VIPListModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    
    //分类数据
    static func loadCateData(completionHandle: @escaping (CateModel) -> ()) {
        let url = BASE_URL + "/sort/mobileCateList"
        let requestParmeters:[String: Any] = ["v": 2]
        Alamofire.request(url, method:.get ,parameters:requestParmeters).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = CateModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //漫画列表
    static func loadComicList(argCon: Int, argName: String, argValue: Int,  page: Int,completionHandle: @escaping (ComiscTypeListModel) -> ()){
        let url = BASE_URL + "/list/commonComicList"
        var parames: [String: Any] = [:]
        parames["argCon"] = argCon
        if argName.count > 0 {
            parames["argName"] = argName
        }
        parames["argValue"] = argValue
        parames["page"] = max(1, page)
        Alamofire.request(url, method:.get, parameters: parames).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = ComiscTypeListModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //漫画详情 ===
    //猜你喜欢
    static func loadLoveData(comicid: Int, completionHandle: @escaping (LoveModel) -> ()) {
        let url = BASE_URL + "/comic/guessLike"
        Alamofire.request(url, method:.get).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = LoveModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //详情（基本）
    static func loadDetailStaticData(comicid: Int, completionHandle: @escaping (DetailStaticModel) -> ()){
        let url = BASE_URL + "/comic/detail_static_new"
        var parames: [String: Any] = [:]
        parames["comicid"] = comicid
        parames["v"] = 3320101
        Alamofire.request(url, method:.get, parameters:parames).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = DetailStaticModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //详情（实时）
    static func loadDetailRealtimeData(comicid: Int, completionHandle: @escaping (DetailRealtimeModel) -> ()){
        let url = BASE_URL + "/comic/detail_realtime"
        var parames: [String: Any] = [:]
        parames["comicid"] = comicid
        parames["v"] = 3320101
        Alamofire.request(url, method:.get, parameters:parames).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = DetailRealtimeModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //评论
    static func loadCommentData(object_id: Int, thread_id: Int, page: Int, completionHandle: @escaping (CommentListModel) -> ()){
        let url = BASE_URL + "/comment/list"
        var parames: [String: Any] = [:]
        parames["object_id"] = object_id
        parames["page"] = page
        parames["thread_id"] = thread_id
        Alamofire.request(url, method:.get, parameters:parames).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                let model = CommentListModel(jsonData: json["data"]["returnData"])
                completionHandle(model)
            }
        }
    }
    
    //章节内容
    static func loadChapterData(chapter_id: Int, completionHandle: @escaping (ChapterModel) -> ()){
        let url = BASE_URL + "/comic/chapterNew"
        var parames: [String: Any] = [:]
        parames["chapter_id"] = chapter_id
        Alamofire.request(url, method:.get, parameters: parames).responseJSON { (response) in
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
                if json["data"]["stateCode"] == -2 {
                    self.warnBar(with: "当前章节为付费章节，请先登录")
                }else if json["data"]["stateCode"] == -4 {
                    self.warnBar(with: "当前章节为Vip，请先登录")
                }else {
                    let model = ChapterModel(jsonData: json["data"]["returnData"])
                    completionHandle(model)
                }
                
            }
        }
    }
    
    
    
    
    static func warnBar(with string: String) {
        let attributedTitle = NSMutableAttributedString(string: string)
        attributedTitle.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 20), range: (string as NSString).range(of: string))
        let banner = NotificationBanner(attributedTitle: attributedTitle)
        banner.backgroundColor = UIColor.orange
        banner.show()
    }
}





